﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<CRM.Database.Model.Customers>" %>
<%@ Import Namespace="CRM.Database.Model" %>
<%@ Import Namespace="CRM.Resourses" %>


<% Html.DevExpress().GridView(settings =>
                    {
                        settings.Name = "gdEquipment";
                        settings.KeyFieldName = "IdCustomersEquipments";

                        settings.CallbackRouteValues =
                                           new { Controller = "Customers", Action = "CustomerEquipments_Edit", customerId=Model.IdCustomer};


                        settings.SettingsEditing.UpdateRowRouteValues = new { Controller = "Customers", Action = "CustomerEquipmentsUpdate", customerId = Model.IdCustomer };
                        settings.SettingsEditing.AddNewRowRouteValues = new { Controller = "Customers", Action = "CustomerEquipmentsAdd", customerId = Model.IdCustomer };
                        settings.SettingsEditing.DeleteRowRouteValues = new { Controller = "Customers", Action = "CustomerEquipmentsDelete", customerId = Model.IdCustomer };

                        settings.SettingsEditing.Mode = GridViewEditingMode.EditFormAndDisplayRow;
                      //  settings.Width = Unit.Percentage(100);
                        
                        settings.SettingsPager.PageSize = 20;

                        settings.CommandColumn.Visible = true;
                        settings.CommandColumn.NewButton.Visible = true;
                       settings.CommandColumn.DeleteButton.Visible = true;
                        settings.CommandColumn.EditButton.Visible = true;
                        settings.SettingsBehavior.ConfirmDelete = true;
                        settings.Settings.ShowFilterRow = false;

                   
                        var fOrd=Model.CustomersEquipments.FirstOrDefault();
                        


                        settings.Columns.Add(column =>
                                                 {
                                                     column.Name = "customDDEquip";
                                                     
                                                     column.Caption = Resourses.Equipment;
                                                     column.FieldName = "CustomEquipment";
                                                     column.ColumnType = MVCxGridViewColumnType.DropDownEdit;
                                                     column.UnboundType = DevExpress.Data.UnboundColumnType.String;
                                                     //var ddeProperties = column.PropertiesEdit as DropDownEditProperties;
                                                     //if (ddeProperties != null)
                                                     //    ddeProperties.ClientInstanceName = "dde";

                                                     column.SetEditItemTemplateContent(c =>
                                                     {
                                                         var value = c.Grid.GetRowValues(c.VisibleIndex, c.Column.FieldName);
                                                         ViewData["ddeEquipmentValue"] = value;
                                                         Html.RenderPartial("DDEquipment");
                                                     });
                                                     //MyCustomSettings customSettings = new MyCustomSettings();
                                                     //customSettings.SetMyTemplateContent((c) =>
                                                     //                                        {
                                                     //                                            var col =new EquipmentsCollection();
                                                     //    Html.RenderPartial("EquipmentsListPartialView", col.EquipmentsList);
                                                     //});

                                                     //((DropDownEditProperties)column.PropertiesEdit).DropDownWindowTemplate =
                                                     //      DevExpress.Web.Mvc.Internal.ContentControlTemplate<TemplateContainerBase>.Create(customSettings.MyTemplateContent, customSettings.MyTemplateContentMethod, typeof(TemplateContainerBase));

                                                 });


                      
                        settings.Columns.Add(column =>
                        {
                            column.Caption = Resourses.EquipmentProductionDate;
                            column.ColumnType = MVCxGridViewColumnType.DateEdit;
                            column.FieldName = "IssueDate";
                        });
                        settings.Columns.Add(column =>
                        {
                            column.Caption = Resourses.EquipmentCount;
                            column.FieldName = "EquipmentCount";
                        });


                        settings.CustomUnboundColumnData = (s, e) =>
                                                               {
                                                                   CustomersEquipments ce =
                                                                       (s as MVCxGridView).GetRow(e.ListSourceRowIndex)
                                                                       as CustomersEquipments;

                                                                   System.Text.StringBuilder value = new System.Text.StringBuilder();

                                                                   if (e.Column.FieldName == "CustomEquipment")
                                                                   {
                                                                       if (ce != null)
                                                                           value.Append(
                                                                               ce.Equipments.EquipmentTypes.EquipmentTypeRu + ", " + ce.Equipments.Vendors.VendorName+ ", "+ ce.Equipments.Model);
                                                                   }

                                                                   e.Value = value.ToString();
                                                               };

                        //settings.SetEditFormTemplateContent(c=>
                        //                                        {
                        //                                            CustomersEquipments customersEquipments = null;
                        //                                            if (ViewData["CustomersEquipments"] != null )
                        //                                            {
                        //                                                customersEquipments =
                        //                                                    ViewData["CustomersEquipments"] as CustomersEquipments;
                        //                                            }
                        //                                            else
                        //                                            {
                        //                                                customersEquipments =new CustomersEquipments();
                        //                                            }

                        //                                            ViewContext.Writer.Write("<table><tr><td>");

                        //                                            ViewContext.Writer.Write("<table><tr><td>" + Resourses.EquipmentType);

                        //                                            Html.RenderAction("EquipmentTypesPartial", "EquipmentCombobox");
                        //                                            ViewContext.Writer.Write("</td><td>" + Resourses.EquipmentVendor);

                        //                                            Html.RenderAction("VendorsPartial", "EquipmentCombobox");
                        //                                            ViewContext.Writer.Write("</td><td>" + Resourses.EquipmentModel);
                        //                                            Html.RenderAction("ModelsPartial", "EquipmentCombobox");
                        //                                            ViewContext.Writer.Write("</td></tr><tr><td>" + Resourses.EquipmentProductionDate);

                        //                                            Html.DevExpress().DateEdit(s =>
                        //                                                                           {
                        //                                                                               s.Name =
                        //                                                                                   "IssueDate";
                        //                                                                               s.Properties.
                        //                                                                                   DisplayFormatString
                        //                                                                                   = "Y";
                        //                                                                           }).Bind(
                        //                                                                               DataBinder.Eval(
                        //                                                                                   customersEquipments,
                        //                                                                                   "IssueDate"))
                        //                                                .Render();

                        //                                            ViewContext.Writer.Write("</td><td>" + Resourses.EquipmentCount);
                        //                                            Html.DevExpress().TextBox(s =>
                        //                                            {
                        //                                                s.Name =
                        //                                                    "EquipmentCount";
                        //                                            }).Bind(
                        //                                                                               DataBinder.Eval(
                        //                                                                                   customersEquipments,
                        //                                                                                   "EquipmentCount"))
                        //                                                .Render();
                        //                                            ViewContext.Writer.Write("</td><td>");
                        //                                            ViewContext.Writer.Write("</td></tr></table>");


                        //                                            ViewContext.Writer.Write("</td></tr><tr><td><table style='width:100%;'><tr><td>");

                        //                                            Html.DevExpress().Button(
                        //                                               btnSettings =>
                        //                                               {
                        //                                                   btnSettings.Name = "btnUpdate1";
                        //                                                   btnSettings.ControlStyle.CssClass = "button";
                        //                                                   btnSettings.Text = Resourses.Save;
                        //                                                   btnSettings.ClientSideEvents.Click = "function(s, e){ gdEquipment.UpdateEdit(); }";
                        //                                               }
                        //                                           )
                        //                                           .Render();

                        //                                            ViewContext.Writer.Write("</td><td>");
                        //                                            Html.DevExpress().Button(
                        //                                                btnSettings =>
                        //                                                {
                        //                                                    btnSettings.Name = "btnCancel";
                        //                                                    btnSettings.ControlStyle.CssClass = "button";
                        //                                                    btnSettings.Text =Resourses.Cancel;
                        //                                                    btnSettings.ClientSideEvents.Click = "function(s, e){ gdEquipment.CancelEdit(); }";
                        //                                                }
                        //                                            )
                        //                                            .Render();

                        //                                            ViewContext.Writer.Write("</td></tr></table></td></tr><table>");

                        //                                        });


                    }

    ).Bind(Model.CustomersEquipments.ToList()).GetHtml();                 %>

   